var t = Object.assign || function(t) {
    for (var i = 1; i < arguments.length; i++) {
        var e, o = arguments[i];
        for (e in o) Object.prototype.hasOwnProperty.call(o, e) && (t[e] = o[e]);
    }
    return t;
}, i = {
    drawBlock: function(t) {
        var i, e = t.text, o = void 0 === (i = t.width) ? 0 : i, s = t.height, n = t.x, r = t.y, h = void 0 === (u = t.paddingLeft) ? 0 : u, a = void 0 === (f = t.paddingRight) ? 0 : f, c = t.borderWidth, x = t.backgroundColor, d = t.borderColor, l = void 0 === (g = t.borderRadius) ? 0 : g, u = void 0 === (i = t.opacity) ? 1 : i, f = 0, g = 0;
        t = 0;
        void 0 !== e ? (f = o < (i = this._getTextWidth("string" == typeof e.text ? e : e.text)) ? i : o, 
        f += h + h, i = void 0 === (i = e.textAlign) ? "left" : i, e.text, t = s / 2 + r, 
        g = "left" === i ? n + h : "center" === i ? f / 2 + n : n + f - a) : f = o, x && (this.ctx.save(), 
        this.ctx.setGlobalAlpha(u), this.ctx.setFillStyle(x), 0 < l ? (this._drawRadiusRect(n, r, f, s, l), 
        this.ctx.fill()) : this.ctx.fillRect(this.toPx(n), this.toPx(r), this.toPx(f), this.toPx(s)), 
        this.ctx.restore()), c && (this.ctx.save(), this.ctx.setGlobalAlpha(u), this.ctx.setStrokeStyle(d), 
        this.ctx.setLineWidth(this.toPx(c)), 0 < l ? (this._drawRadiusRect(n, r, f, s, l), 
        this.ctx.stroke()) : this.ctx.strokeRect(this.toPx(n), this.toPx(r), this.toPx(f), this.toPx(s)), 
        this.ctx.restore()), e && this.drawText(Object.assign(e, {
            x: g,
            y: t
        }));
    },
    drawText: function(i) {
        var e, o = this, s = i.x, n = i.y, r = (i.fontSize, i.color, i.baseLine), h = (i.textAlign, 
        i.text);
        i.opacity, i.width, i.lineNum, i.lineHeight, "[object Array]" === Object.prototype.toString.call(h) ? (e = {
            x: s,
            y: n,
            baseLine: r
        }, h.forEach(function(i) {
            e.x += i.marginLeft || 0;
            var s = o._drawSingleText(Object.assign(i, t({}, e)));
            e.x += s + (i.marginRight || 0);
        })) : this._drawSingleText(i);
    },
    drawImage: function(t) {
        var i = t.imgPath, e = t.x, o = t.y, s = t.w, n = t.h, r = t.sx, h = t.sy, a = t.sw, c = t.sh, x = void 0 === (d = t.borderRadius) ? 0 : d, d = void 0 === (d = t.borderWidth) ? 0 : d;
        t = t.borderColor;
        this.ctx.save(), 0 < x ? (this._drawRadiusRect(e, o, s, n, x), this.ctx.strokeStyle = "rgba(255,255,255,0)", 
        this.ctx.stroke(), this.ctx.clip(), this.ctx.drawImage(i, this.toPx(r), this.toPx(h), this.toPx(a), this.toPx(c), this.toPx(e), this.toPx(o), this.toPx(s), this.toPx(n)), 
        0 < d && (this.ctx.setStrokeStyle(t), this.ctx.setLineWidth(this.toPx(d)), this.ctx.stroke())) : this.ctx.drawImage(i, this.toPx(r), this.toPx(h), this.toPx(a), this.toPx(c), this.toPx(e), this.toPx(o), this.toPx(s), this.toPx(n)), 
        this.ctx.restore();
    },
    drawLine: function(t) {
        var i = t.startX, e = t.startY, o = t.endX, s = t.endY, n = t.color;
        t = t.width;
        this.ctx.save(), this.ctx.beginPath(), this.ctx.setStrokeStyle(n), this.ctx.setLineWidth(this.toPx(t)), 
        this.ctx.moveTo(this.toPx(i), this.toPx(e)), this.ctx.lineTo(this.toPx(o), this.toPx(s)), 
        this.ctx.stroke(), this.ctx.closePath(), this.ctx.restore();
    },
    downloadResource: function(t) {
        var i = this, e = void 0 === (e = t.images) ? [] : e, o = void 0 === (t = t.pixelRatio) ? 1 : t, s = [];
        return this.drawArr = [], (e = e.filter(function(t) {
            return t.url;
        })).forEach(function(t, e) {
            return s.push(i._downloadImageAndInfo(t, e, o));
        }), Promise.all(s);
    },
    initCanvas: function(t, i, e) {
        var o = this;
        return new Promise(function(s) {
            o.setData({
                pxWidth: o.toPx(t),
                pxHeight: o.toPx(i),
                debug: e
            }, s);
        });
    }
}, e = {
    _drawRadiusRect: function(t, i, e, o, s) {
        s /= 2, this.ctx.beginPath(), this.ctx.moveTo(this.toPx(t + s), this.toPx(i)), this.ctx.lineTo(this.toPx(t + e - s), this.toPx(i)), 
        this.ctx.arc(this.toPx(t + e - s), this.toPx(i + s), this.toPx(s), 2 * Math.PI * .75, 2 * Math.PI * 1), 
        this.ctx.lineTo(this.toPx(t + e), this.toPx(i + o - s)), this.ctx.arc(this.toPx(t + e - s), this.toPx(i + o - s), this.toPx(s), 0, 2 * Math.PI * .25), 
        this.ctx.lineTo(this.toPx(t + s), this.toPx(i + o)), this.ctx.arc(this.toPx(t + s), this.toPx(i + o - s), this.toPx(s), 2 * Math.PI * .25, 2 * Math.PI * .5), 
        this.ctx.lineTo(this.toPx(t), this.toPx(i + s)), this.ctx.arc(this.toPx(t + s), this.toPx(i + s), this.toPx(s), 2 * Math.PI * .5, 2 * Math.PI * .75);
    },
    _getTextWidth: function(t) {
        var i = this, e = [];
        "[object Object]" === Object.prototype.toString.call(t) ? e.push(t) : e = t;
        var o = 0;
        return e.forEach(function(t) {
            var e = t.fontSize, s = t.text, n = void 0 === (n = t.marginLeft) ? 0 : n;
            t = void 0 === (t = t.marginRight) ? 0 : t;
            i.ctx.setFontSize(i.toPx(e)), o += i.ctx.measureText(s).width + n + t;
        }), this.toRpx(o);
    },
    _drawSingleText: function(t) {
        var i = this, e = t.x, o = t.y, s = t.fontSize, n = t.color, r = t.baseLine, h = void 0 === (x = t.textAlign) ? "left" : x, a = t.text, c = void 0 === (g = t.opacity) ? 1 : g, x = void 0 === (f = t.textDecoration) ? "none" : f, d = t.width, l = void 0 === (g = t.lineNum) ? 1 : g, u = void 0 === (f = t.lineHeight) ? 0 : f, f = void 0 === (g = t.fontWeight) ? "normal" : g, g = void 0 === (g = t.fontStyle) ? "normal" : g;
        t = void 0 === (t = t.fontFamily) ? "sans-serif" : t;
        this.ctx.save(), this.ctx.beginPath(), this.ctx.font = g + " " + f + " " + this.toPx(s, !0) + "px " + t, 
        this.ctx.setGlobalAlpha(c), this.ctx.setFillStyle(n), this.ctx.setTextBaseline(r), 
        this.ctx.setTextAlign(h);
        var P = [];
        if (d < (h = this.toRpx(this.ctx.measureText(a).width))) {
            for (var p = "", v = 1, m = 0; m <= a.length - 1; m++) p += a[m], this.toRpx(this.ctx.measureText(p).width) >= d ? (v === l && m !== a.length - 1 && (p = p.substring(0, p.length - 1) + "..."), 
            v <= l && P.push(p), p = "", v++) : v <= l && m === a.length - 1 && P.push(p);
            h = d;
        } else P.push(a);
        if (P.forEach(function(t, n) {
            i.ctx.fillText(t, i.toPx(e), i.toPx(o + (u || s) * n));
        }), this.ctx.restore(), "none" !== x) {
            var w = o;
            if ("line-through" === x) switch (w = o, r) {
              case "top":
                w += s / 2 + 5;
                break;

              case "middle":
                break;

              case "bottom":
                w -= s / 2 + 5;
                break;

              default:
                w -= s / 2 - 5;
            }
            this.ctx.save(), this.ctx.moveTo(this.toPx(e), this.toPx(w)), this.ctx.lineTo(this.toPx(e) + this.toPx(h), this.toPx(w)), 
            this.ctx.setStrokeStyle(n), this.ctx.stroke(), this.ctx.restore();
        }
        return h;
    }
}, o = {
    _downloadImageAndInfo: function(t, i, e) {
        var o = this;
        return new Promise(function(s, n) {
            var r = t.x, h = t.y, a = t.url, c = t.zIndex;
            o._downImage(a, i).then(function(t) {
                return o._getImageInfo(t, i);
            }).then(function(n) {
                var a = n.imgPath, x = n.imgInfo, d = void 0, l = void 0, u = t.borderRadius || 0, f = t.width, g = t.height;
                (n = o.toRpx(x.width / e)) / (x = o.toRpx(x.height / e)) <= f / g ? (d = 0, l = (x - n / f * g) / 2) : (l = 0, 
                d = (n - x / g * f) / 2), o.drawArr.push({
                    type: "image",
                    borderRadius: u,
                    borderWidth: t.borderWidth,
                    borderColor: t.borderColor,
                    zIndex: void 0 !== c ? c : i,
                    imgPath: a,
                    sx: d,
                    sy: l,
                    sw: n - 2 * d,
                    sh: x - 2 * l,
                    x: r,
                    y: h,
                    w: f,
                    h: g
                }), s();
            }).catch(function(t) {
                console.error("ERROR: ", t), n(t);
            });
        });
    },
    _downImage: function(t) {
        var i = this;
        return new Promise(function(e, o) {
            /^http/.test(t) && !new RegExp(wx.env.USER_DATA_PATH).test(t) ? wx.downloadFile({
                url: i._mapHttpToHttps(t),
                success: function(t) {
                    200 === t.statusCode ? e(t.tempFilePath) : o(t.errMsg);
                },
                fail: function(t) {
                    o(t);
                }
            }) : e(t);
        });
    },
    _getImageInfo: function(t, i) {
        return new Promise(function(e, o) {
            wx.getImageInfo({
                src: t,
                success: function(o) {
                    e({
                        imgPath: t,
                        imgInfo: o,
                        index: i
                    });
                },
                fail: function(t) {
                    o(t);
                }
            });
        });
    },
    toPx: function(t, i) {
        return i ? parseInt(t * this.factor * this.pixelRatio) : t * this.factor * this.pixelRatio;
    },
    toRpx: function(t, i) {
        return i ? parseInt(t / this.factor) : t / this.factor;
    },
    _mapHttpToHttps: function(t) {
        if (t.indexOf(":") < 0) return t;
        var i = t.split(":");
        return 2 === i.length && "http" === i[0] ? (i[0] = "https", i[0] + ":" + i[1]) : t;
    }
};

Component({
    properties: {},
    created: function() {
        var t = wx.getSystemInfoSync().screenWidth;
        this.factor = t / 750;
    },
    methods: Object.assign({
        getHeight: function(i) {
            function e(t) {
                var i = t.lineHeight || t.fontSize;
                return "top" === t.baseLine ? i : "middle" === t.baseLine ? i / 2 : 0;
            }
            var o = [];
            (i.blocks || []).forEach(function(t) {
                o.push(t.y + t.height);
            }), (i.texts || []).forEach(function(i) {
                var s = void 0;
                "[object Array]" === Object.prototype.toString.call(i.text) ? i.text.forEach(function(n) {
                    s = e(t({}, n, {
                        baseLine: i.baseLine
                    })), o.push(i.y + s);
                }) : (s = e(i), o.push(i.y + s));
            }), (i.images || []).forEach(function(t) {
                o.push(t.y + t.height);
            }), (i.lines || []).forEach(function(t) {
                o.push(t.startY), o.push(t.endY);
            });
            var s = o.sort(function(t, i) {
                return i - t;
            }), n = 0;
            return 0 < s.length && (n = s[0]), i.height < n || !i.height ? n : i.height;
        },
        create: function(t) {
            var i = this;
            return new Promise(function(e, o) {
                i.ctx = wx.createCanvasContext("canvasid", i), i.pixelRatio = t.pixelRatio || 1;
                var s = i.getHeight(t);
                return i.initCanvas(t.width, s, t.debug).then(function() {
                    t.backgroundColor && (i.ctx.save(), i.ctx.setFillStyle(t.backgroundColor), i.ctx.fillRect(0, 0, i.toPx(t.width), i.toPx(s)), 
                    i.ctx.restore());
                    var n = void 0 === (h = t.texts) ? [] : h, r = t.images, h = (r = void 0 === (h = t.blocks) ? [] : h, 
                    void 0 === (h = t.lines) ? [] : h);
                    (h = i.drawArr.concat(n.map(function(t) {
                        return t.type = "text", t.zIndex = t.zIndex || 0, t;
                    })).concat(r.map(function(t) {
                        return t.type = "block", t.zIndex = t.zIndex || 0, t;
                    })).concat(h.map(function(t) {
                        return t.type = "line", t.zIndex = t.zIndex || 0, t;
                    }))).sort(function(t, i) {
                        return t.zIndex - i.zIndex;
                    }), h.forEach(function(t) {
                        "image" === t.type ? i.drawImage(t) : "text" === t.type ? i.drawText(t) : "block" === t.type ? i.drawBlock(t) : "line" === t.type && i.drawLine(t);
                    });
                    var a = "android" === wx.getSystemInfoSync().platform ? 300 : 100;
                    i.ctx.draw(!1, function() {
                        setTimeout(function() {
                            wx.canvasToTempFilePath({
                                canvasId: "canvasid",
                                success: function(t) {
                                    e(t.tempFilePath), i.triggerEvent("success", t.tempFilePath);
                                },
                                fail: function(t) {
                                    o(t), i.triggerEvent("fail", t);
                                }
                            }, i);
                        }, a);
                    });
                }).catch(function(t) {
                    o(t), console.error(t);
                });
            });
        }
    }, i, e, o)
});